package antlr4.definitive.d4_expr.d4_1_0_expr.run;

import antlr4.definitive.d4_expr.d4_1_0_expr.gen.ExprLexer;
import antlr4.definitive.d4_expr.d4_1_0_expr.gen.ExprParser;
import cn.hutool.core.io.FileUtil;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        CodePointCharStream codePointCharStream;
        if (args.length > 0) {
            String arg = args[0];
            String content = FileUtil.readString(arg, "UTF-8");
            codePointCharStream = CharStreams.fromString(content);
        } else {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入一个 四则运算:");
            String input = scanner.nextLine();
            codePointCharStream = CharStreams.fromString(input);
        }

        //新建一个词法分析器，处理输入的CharStream
        ExprLexer lexer = new ExprLexer(codePointCharStream);
        //新建一个词法符号的缓冲区，用于存储词法分析器将生成的词法符号
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        //新建一个语法分析器，处理词法符号缓冲区中的内容
        ExprParser parser = new ExprParser(tokens);
        ParseTree tree = parser.prog();//针对prog规则，开始语法分析
        System.out.println(tree.toStringTree(parser));//用LIsP风格打印生成的树
    }

}
